﻿using MyCompanyName.AbpZeroTemplate.Prm.Authorization;
using MyCompanyName.AbpZeroTemplate.Prm.Project.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.Runtime.Session;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Project
{
    public class PermissionAppService : AbpZeroTemplateAppServiceBase, IPermissionAppService
    {
        //Abp.Runtime.Session.IAbpSession AbpSession { get; set; }
        private readonly PermissionService _permissionService;
        private readonly ProjectManager _projectManager;

        public PermissionAppService(PermissionService permissionService, ProjectManager projectManager)
        {
            _permissionService = permissionService;
            AbpSession = Abp.Runtime.Session.NullAbpSession.Instance;
            _projectManager = projectManager;
        }

        /// <summary>
        /// 项目首页  新增项目、项目首页查看权限
        /// </summary>
        public bool HasAddProject()
        {
            long userId = AbpSession.GetUserId();
            var hasAuth = _permissionService.HasPermission(userId, 0, ProjectFunctions.项目新增);
            return hasAuth;
        }


        public Permission.GetDetailPermissionOutPut GetDetailPermissionOutPut(int projectId)
        {
            long userId = AbpSession.GetUserId();

            var inquiryCount = _projectManager.GetInquiryCount(projectId);

            var outPut = new Permission.GetDetailPermissionOutPut()
            {
                InquiryCount = inquiryCount,
                HasView = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目查看),
                HasDocument = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目文档),
                HasEditCost = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目成本编辑),
                HasEditInfo = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目信息编辑),
                HasEditProgress = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目进度编辑),
                HasEditTeam = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目成员编辑),
                HasEditPhase = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目阶段编辑),
                HasPlan = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目计划),
                HasFollowered = _permissionService.HasFollowedProject(userId, projectId),
                HasTopic = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目讨论),
                HasDeleteTeam = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目成员删除),
                HasSynthView = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目工艺查看),
                HasAddTask = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题新增) || _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题超级权限),
                HasFollowerTask = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题关注) || _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题超级权限),
                HasTaskExport = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题导出) || _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题超级权限),
                HasTaskArchived = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题归档) || _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目问题超级权限)
            };
            return outPut;
        }


        public GetDocumentAuth GetHasSetType(int projectId)
        {
            long userId = AbpSession.GetUserId();
            var outPut = new GetDocumentAuth()
            {
                HasSetType = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目文档类型设置),
                HasTrash = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目文档回收站)
            };
            return outPut;
        }

        public GetDocumentAuth GetHasAuth(int typeId, int projectId)
        {
            long userId = AbpSession.GetUserId();

            GetDocumentAuth docAuth = new GetDocumentAuth()
            {
                HasUpload = _permissionService.HasPermission(projectId, userId, typeId, DocmentFunctions.文档上传),
                HasDelete = _permissionService.HasPermission(projectId, userId, typeId, DocmentFunctions.文档删除),
                HasDownload = _permissionService.HasPermission(projectId, userId, typeId, DocmentFunctions.文档下载),
                MultipleFiles = _permissionService.IsMultipleUpload(typeId)
            };
            return docAuth;
        }


        public Permission.GetDetailPermissionOutPut GetSynthPermissionOutPut(int projectId)
        {
            long userId = AbpSession.GetUserId();
            var outPut = new Permission.GetDetailPermissionOutPut()
            {
                HasSynthAdd = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目工艺新增),
                HasSynthEdit = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目工艺编辑),
                HasSynthDelete = _permissionService.HasPermission(userId, projectId, ProjectFunctions.项目工艺删除)
            };
            return outPut;
        }
    }
}
